package com.rayboo.example.java8;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * StreamFilter 示例
 *
 * @author ray
 * @since 2019/07/29
 *
 * <p>Java流过滤器。filter()是一个中间操作，它从流中读取数据并在根据给定条件转换数据后返回新流。
 *
 * <p>https://beginnersbook.com/2017/10/java-8-stream-filter/
 */
public class StreamFilterExample {

    public static void main(String[] args) {

        List<String> names = Arrays.asList("Melisandre", "Sansa", "Jon", "Daenerys", "Joffery");

        System.out.println("=========== example-1 ===========");
        //Creating the java8 of all names
        Stream<String> allNames = names.stream();

        //Creating another java8 by filtering long names using filter()
        Stream<String> longNames = allNames.filter(str -> str.length() > 6);

        //displaying the long names
        longNames.forEach(str -> System.out.print(str + " "));


        System.out.println("\n=========== example-2 ===========");
        List<String> longnames = names.stream()
                .filter(str -> str.length() > 6 && str.length() < 8) //Multiple conditions
                .collect(Collectors.toList());

        longnames.forEach(System.out::println);

        System.out.println("=========== example-3 ===========");
        List<Integer> num = Arrays.asList(1,2,3,4,5,6);
        List<Integer> squares = num.stream()
                .map(n -> n * n)
                .collect(Collectors.toList());
        System.out.println(squares);

        System.out.println("=========== example-4 ===========");
        Map<Integer, String> hmap = new HashMap<Integer, String>();
        hmap.put(11, "Apple");
        hmap.put(22, "Orange");
        hmap.put(33, "Kiwi");
        hmap.put(44, "Banana");

        Map<Integer, String> result = hmap.entrySet()
                .stream()
                .filter(map -> map.getKey().intValue() <= 22)
                .collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue()));

        System.out.println("Result: " + result);


        System.out.println("=========== example-5 ===========");
        Map<Integer, String> integerStringMap = hmap.entrySet()
                .stream()
                .filter(map -> "Orange".equals(map.getValue()))
                .collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue()));

        System.out.println("Result: " + integerStringMap);

        System.out.println("=========== example-6 ===========");
        List<String> stringList = Arrays.asList("Java", "Stream", null, "Filter", null);
        List<String> strings = stringList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        strings.forEach(System.out::println);

        System.out.println("=========== example-7 ===========");
        List<Integer> integerList = Arrays.asList(1, 2, 3, null, 4, null, 5);
        List<Integer> list = integerList.stream()
                .map(n -> n) //here you will be having a different logic
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        list.forEach(System.out::println);
    }
}
